package com.xpgk.mvc.loseWeightCustomerRanking;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.kit.LogKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.Render;
import com.jfinal.render.RenderException;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.tools.ToolSqlXml;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class LoseWeightCustomerRankingXLS extends Render {

	private static final String CONTENT_TYPE = "application/msexcel;charset=UTF-8";

	private final List<Record> productData;
	private final List<Record> data;
	private final String fileName;
	private final String search;

	public LoseWeightCustomerRankingXLS(List<Record> productData ,List<Record> data, String fileName,String search) {
		this.data = data;
		this.fileName = fileName;
		this.search = search;
		this.productData=productData;
	}

	@Override
	public void render() {
		this.response.reset();
		try {
			this.response.setHeader("Content-disposition",
					"attachment; filename*=UTF-8''" + URLEncoder.encode(this.fileName, "UTF-8") + ".xls");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		this.response.setContentType(CONTENT_TYPE);
		OutputStream os = null;
		try {
			os = this.response.getOutputStream();
			WritableWorkbook workbook = Workbook.createWorkbook(os);
			WritableSheet sheet = workbook.createSheet(this.fileName, 0);
			//第一行表头
			List<String> one_header = new ArrayList<>();
			one_header.add("日期");
			one_header.add("排名");
			one_header.add("姓名");
			one_header.add("掉秤");
			one_header.add("组别");
			one_header.add("责任人");
			one_header.add("美体师");
			for(int i =0;i<productData.size();i++){
				one_header.add("做产品");
			}
			one_header.add("备注");
			//第二行表头
			List<String> two_header = new ArrayList<>();
			two_header.add("日期");
			two_header.add("排名");
			two_header.add("姓名");
			two_header.add("掉秤");
			two_header.add("组别");
			two_header.add("责任人");
			two_header.add("美体师");
			for(Record r : productData){
				two_header.add(r.getStr("name"));
			}
			two_header.add("备注");
			//列名
			List<String> columns = new ArrayList<>();
			columns.add("date");
			columns.add("paiming");
			columns.add("date1");
			columns.add("date2");
			columns.add("zubie");
			columns.add("date4");
			columns.add("date5");
			for(Record r : productData){
				columns.add(r.getStr("ids"));
			}
			columns.add("bz");
			int row = 0;//行数

			for(int i =0;i<7+productData.size();i++){
				sheet.setColumnView(i, 15); // 设置列的宽度
			}
			for(int i =0;i<=data.size()+3;i++){
				sheet.setRowView(i, 600); // 设置行的高度
			}
			//合并单元格
			sheet.mergeCells(0, 0, 7+productData.size(), 0);
			sheet.mergeCells(1, 1, 7+productData.size(), 1);
			sheet.mergeCells(7, 2, 6+productData.size(), 2);
			for(int i =0;i<7;i++){
				sheet.mergeCells(i, 2, i, 3);
			}
			sheet.mergeCells(7+productData.size(), 2, 7+productData.size(), 3);
			//表头样式
			WritableCellFormat header_format = new WritableCellFormat();
			header_format.setBackground(Colour.GRAY_25);// 背景色
			header_format.setAlignment(Alignment.CENTRE);// 水平对齐
			header_format.setVerticalAlignment(VerticalAlignment.CENTRE);// 垂直对齐
			header_format.setBorder(Border.ALL, BorderLineStyle.THIN);// 边框
			header_format.setWrap(true);// 是否换行
			sheet.addCell(new Label(0,row,fileName,header_format));
			row++;
			sheet.addCell(new Label(0,row,"搜索条件：",header_format)); 
			sheet.addCell(new Label(1,row,search,header_format));
			row++;
			//生成第一行表头
			for (int i = 0; i < one_header.size(); i++) {
				String header = one_header.get(i);
				sheet.addCell(new Label(i, row, header, header_format));
			}
			row++;
			//生成第二行表头
			for (int i = 0; i < two_header.size(); i++) {
				String header = two_header.get(i);
				sheet.addCell(new Label(i, row, header, header_format));
			}
			row++;
			//表格样式
			WritableCellFormat row_format = new WritableCellFormat();
			row_format.setAlignment(Alignment.CENTRE);// 水平对齐
			row_format.setVerticalAlignment(VerticalAlignment.CENTRE);// 垂直对齐
			row_format.setBorder(Border.ALL, BorderLineStyle.THIN);// 边框
			//生成表
			for (int j = 0; j < data.size(); j++) {
				Record record = (Record) data.get(j);
				String[] values = getValues(columns, record);
				for (int i = 0; i < values.length; i++) {
					sheet.addCell(new Label(i, row, values[i], row_format));
				}
				row++;
			}
			workbook.write();
			workbook.close();
		} catch (IOException e) {
			throw new RenderException(e);
		} catch (WriteException e) {
			e.printStackTrace();
		} finally {
			try {
				if (os != null) {
					os.flush();
					os.close();
				}
			} catch (IOException e) {
				LogKit.error(e.getMessage(), e);
			}
		}
	}
	private static String[] getValues(List<String> columns, Record record) {
		String[] values = new String[columns.size()];
		int i = -1;
		for (String col : columns) {
			i++;
			Object value = record.get(col);
			if (value == null) {
				continue;
			}
			values[i] = value.toString();
		}
		return values;
	}
}